package com.huang.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.eduservice.entity.EduCourse;
import com.huang.eduservice.entity.vo.CourseInfVo;
import com.huang.eduservice.entity.vo.CoursePublishVo;
import com.huang.eduservice.entity.vo.CourseQuery;
import com.huang.eduservice.service.EduCourseService;
import com.hunag.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-21
 */
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {

    @Autowired
    private EduCourseService service;
    @PostMapping("addCourseInf")
    public  R addCouseInf(@RequestBody CourseInfVo courseInfVo){
       String id= service.saveCourseVo(courseInfVo);

        return R.ok().data("courseId",id);

    }

    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
     CourseInfVo courseInfVo=service.getCourseInfo(courseId);
        return R.ok().data("courseInfVo",courseInfVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfVo courseInfVo){
       service.updateCourseInfo(courseInfVo);
        return R.ok();

    }

    @GetMapping("getPublish/{id}")
    public R getPublish(@PathVariable String id){
       CoursePublishVo coursePublishVo= service.getPublishVo(id);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    @PostMapping("updateStatus/{id}")
    public R updateStatus(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        service.updateById(eduCourse);
        return R.ok();
    }



    @PostMapping("getAllCourse/{current}/{limit}")
    public R getAllCourse(
             @PathVariable Integer current
            ,@PathVariable Integer limit,@RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> eduCoursePage = new Page<>(current,limit);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(courseQuery.getTitle())){
                wrapper.like("title",courseQuery.getTitle());
            }
            if(!StringUtils.isEmpty(courseQuery.getStatus())){
                wrapper.eq("status",courseQuery.getStatus());
            }
        IPage page = service.page(eduCoursePage, wrapper);
        List records = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("records",records).data("total",total);
    }

    @DeleteMapping("deleteCourseWith/{id}")
    public R deleteCourseWith(@PathVariable String id){
        service.removeCourse(id);
        return  R.ok();

    }

}

